float x = 10;
float y = 10;
float i = random(10, height - 10);
float j = random(10, width - 10);

float koordX = 0;

int n = 5;
int m = 5;

int MoveRightFlag = 0;
int MoveLeftFlag = 0;
int MoveUpFlag = 0;
int MoveDownFlag = 0;
int WallFlag = 0;

float[][] balls;
int AppleCount = 1;
float Speed = 2;



//=================================================

void setup() {
  size(400,400);
  frameRate(30);
  smooth();
  
  balls = new float[50][2];
  for(int n=0; n<1; n++) {
      balls[n][0] = x;
      balls[n][1] = y;
  }
  for(int n=1; n<50; n++) {
      balls[n][0] = 0;
      balls[n][1] = 0;
  }
  
  /*
  for (int n = 0; n <= AppleCoutn; n++){
    ellipse(balls[n][0], balls[n][1], 10, 10);
  }*/

}


void draw() {
  background(0);
 
  

  // Draw the ellipse at the cartesian coordinate
  ellipseMode(CENTER);
  noStroke();
  fill(200);
  
  for(int n = 0; n < AppleCount - 1; n++){
    ellipse(balls[n][0], balls[n][1], 10, 10);
  }
 
  if (MoveUpFlag == 1){
    y = y - Speed;
    if (y < 0){
      y = 0;
      WallFlag = 1;
    }
    MoveDownFlag = 0;
  }
  if (MoveDownFlag == 1){
    y = y + Speed;
    if (y > width){
      y = width;
      WallFlag = 1;
    }
    MoveUpFlag = 0;
  }
  if (MoveRightFlag == 1){
    x = x + Speed;
    if (x > height){
      x = height;
      WallFlag = 1;
    }
    MoveLeftFlag = 0;
  }
  if (MoveLeftFlag == 1){
    x = x - Speed;
    if (x < 0){
      x = 0;
      WallFlag = 1;
    }
    MoveRightFlag = 0;
  }



  
  //warm eatting the apple
  if (((x <= j + 7) && (y >= i - 7)) && ((y <= i + 7) && (x >= j - 7))){
    if (Speed < 15){
      Speed = Speed + 0.5;
    }
    i = random(height - 10);
    j = random(width - 10);
    AppleCount = AppleCount + 1;
    for(n = AppleCount - 1; n > 0; n--){
      balls[n][0] = balls[n-1][0];
      balls[n][1] = balls[n-1][1];
    }
    balls[0][0] = x;
    balls[0][1] = y;
      
  }  
  
  rect(j, i, 7, 7); 
  
  if (WallFlag == 0){
    for(n = AppleCount - 1; n > 0; n--){
      balls[n][0] = balls[n-1][0];
      balls[n][1] = balls[n-1][1];
    }
    balls[0][0] = x;
    balls[0][1] = y;
  }else{
    for(n = AppleCount - 1; n > 0; n--){
      balls[n][0] = balls[n][0];
      balls[n][1] = balls[n][1];
    }
    balls[0][0] = x;
    balls[0][1] = y;
    WallFlag = 0;
  }
  
  if (AppleCount == 50){
    javax.swing.JOptionPane.showMessageDialog(null, "Вы выигралииии!!!! \n Ваши баллы: " + AppleCount*10);  
    exit();
  }
  
  
  if (AppleCount > 3){
    for(n = 3; n < AppleCount - 1; n++){
        if (balls[n][0] == balls[0][0] && balls[n][1] == balls[0][1]){
          javax.swing.JOptionPane.showMessageDialog(null, "Вы проиграли, попробуйте снова");  
          exit();
        }
    }
  }

}





//================================================================
//direction movies
void keyPressed()
{
  if (key == CODED) {
    if(keyCode == UP && MoveDownFlag == 0) {
      MoveUpFlag = 1;
      MoveDownFlag = 0;
      MoveRightFlag = 0;
      MoveLeftFlag = 0;
    } 
    if (keyCode == DOWN && MoveUpFlag == 0) {
      MoveUpFlag = 0;
      MoveDownFlag = 1;
      MoveRightFlag = 0;
      MoveLeftFlag = 0;
    }
    if(keyCode == LEFT && MoveRightFlag == 0) {
      MoveUpFlag = 0;
      MoveDownFlag = 0;
      MoveRightFlag = 0;
      MoveLeftFlag = 1;
    } 
    if (keyCode == RIGHT && MoveLeftFlag == 0) {
      MoveUpFlag = 0;
      MoveDownFlag = 0;
      MoveRightFlag = 1;
      MoveLeftFlag = 0;
    }
    if (keyCode == SHIFT){
      javax.swing.JOptionPane.showMessageDialog(null, "Пауза");      
    }
  }

}
